package view;


import java.awt.Color;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.text.*;


/**
 * CurrentLineHighlighter
 * Usage: textComp.addCaretListener(new CurrentLineHighlighter());
 * @author Adriano de Albuquerque Batista
 * @author Alfeu Buriti
 * @author Pablo Diego Alencar
 * @author Yuri Cariry Cabral de Melo
 */
public class CurrentLineHighlighter implements CaretListener
{
	
	static final Color DEFAULT_COLOR = new Color(0, 0, 0, 40);
	private Highlighter.HighlightPainter painter;
	private Object highlight;

	
	/**
	 * Construtor CurrentLineHighlighter vazio
	 */
	public CurrentLineHighlighter(){
	}

	
	/**
	 * CurrentLineHighlighter
	 * @param highlightColor
	 */
	public CurrentLineHighlighter(Color highlightColor){
		Color c = highlightColor != null ? highlightColor : DEFAULT_COLOR;
		painter = new DefaultHighlighter.DefaultHighlightPainter(c);
	}

	/**
	 * caretUpdate
	 */
	public void caretUpdate(CaretEvent evt)
	{
		JTextComponent comp = (JTextComponent)evt.getSource();
		if (comp != null && highlight != null)
		{
			comp.getHighlighter().removeHighlight(highlight);
			highlight = null;
		}

		int pos = comp.getCaretPosition();
		/* Element elem = Utilities.getParagraphElement(comp, pos);
        int start = elem.getStartOffset();
        int end = elem.getEndOffset();
		 */
		try
		{
			int start = Utilities.getRowStart(comp, pos);
			int end = Utilities.getRowEnd(comp, pos);
			highlight = comp.getHighlighter().addHighlight(start, end,
					painter);
		}
		catch (BadLocationException ex)
		{
			ex.printStackTrace();
		}
	}
	
	
	
}

